﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Plugins.ContentTree;
using System.Web.Mvc;
using MethodWorx.CMS.Plugins.ActionResults;
using MethodWorx.CMS.Providers;
using MethodWorx.CMS.TemplateLanguage;
using MethodWorx.CMS.TemplateLanguage.Properties;

namespace MethodWorx.CMS.Plugins.GlobalProperties
{
    [CmsPlugin(
     Id = "{CB060086-CDC0-4587-BFF0-FF35A78A5CDF}",
     Name = "Global Properties",
     Description = "Maintains global properties")]
    public class GlobalPropertiesExtension : Plugin, IAutoLoadedPlugin, IContentTreePlugin
    {
        

        public GlobalPropertiesExtension()
        {
            this.RegisterIdeExtension(this.ideExtension);

            GlobalPropertyParser.Plugin = this;
            //DefaultParserFactory.Add(new GlobalPropertyParser());
        }

        public IEnumerable<TreeNode> GetTreeNodes(string objectType, Guid? objectId)
        {
            if (string.IsNullOrEmpty(objectType) && objectId == null)
            {
                yield return new TreeNode()
                                 {
                                     Title = "Global Properties",
                                     Icon = "~/Images/icons/table.png",
                                     ObjectId = new Guid("{BE44779F-C645-494F-B577-FD88D4143B7C}"),
                                     ObjectType = "GlobalProperties",
                                     ToolbarButtons = new ToolbarButton[]
                                                          {

                                                              new ToolbarButton()
                                                                  {
                                                                      Icon = "~/Images/Icons/Table_add.png",
                                                                      Title = "Create Global Property",
                                                                      Id = "CreateGlobalProperty"
                                                                  }
                                                          }
                                 };

            }
            else if(objectType == "GlobalProperties")
            {
                //
                //  enumerate all the global properties
                var service = new PluginService(this.CmsDataProvider);
                foreach(var d in  service.GetAllUserData<object>(this, null, "GlobalProperty"))
                {
                    //
                    //  ok, we can cast the object here
                    //  to a property details
                    var name = (string)d.UserData.GetProperty("Name");
                    yield return new TreeNode()
                                     {
                                         Title = name,
                                         Icon = "~/Images/icons/html.png",
                                         ObjectId = d.Id,
                                         ObjectType = "GlobalProperty",
                                         DefaultAction = "EditGlobalProperty",
                                         ToolbarButtons = new ToolbarButton[]
                                                          {

                                                              new ToolbarButton()
                                                                  {
                                                                      Icon = "~/Images/Icons/Table_edit.png",
                                                                      Title = "Edit Global Property",
                                                                      Id = "EditGlobalProperty"
                                                                  }
                                                          }
                                     };
                }
            }
        }

        private IdeExtension ideExtension = new IdeExtension()
        {
            Id = new Guid("{CB060086-CDC0-4587-BFF0-FF35A78A5CDF}"),
            DisplayName = "Global Properties",
            Name = "Global Properties",
            WebEditor = new AssemblyResourceReference(typeof(GlobalPropertiesExtension).Assembly, "Plugins/Views/GlobalProperties/Create.ascx"),
            Controller = typeof(GlobalPropertiesController)
        };

        


        public System.Web.Mvc.ActionResult InvokeAction(string action, string objectType, Guid? objectId)
        {
            switch(action)
            {
                case "CreateGlobalProperty":
                    return new OpenDialogResult(PluginUrlHelper.GlobalPluginAction(this.ideExtension, "Create"), "Create Global Property");
                    break;
                case "EditGlobalProperty":
                    return
                        new OpenDialogResult(
                            PluginUrlHelper.GlobalPluginAction(this.ideExtension, "Edit") + "?id=" + objectId,
                            "Edit Global Property");
                    break;
            }
            return new EmptyResult();
        }

    }
}
